home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / IFF / Old_IFF_Packages / November_1988 / Examples / PGTB / tbwutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-12  |  3.8 KB  |  155 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\  The Software Distillery                         */
  3. /* |. o.| || Made available to the Amiga development community             */
  4. /* | .    | || the authors:                       BBS:      */
  5. /* | o    | ||   Jim Cooper, John Mainwaring             (919)-471-6436  */
  6. /* |  . |//                                     */
  7. /* ======                                     */
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. /* routines used during output of traceback    */
  11.  
  12. #include "tb.h"
  13.  
  14. extern struct tbtemplate *tbdata;
  15.  
  16. int valid_display(char);
  17. void hexconv(unsigned char *, unsigned char);
  18.  
  19. int valid_display(byte) /* return filtered ASCII char */
  20. /*  -------------    */
  21. char byte;
  22.  
  23. /* Filter all CTRL and CSI chars which could mess things up */
  24. {
  25.   if (((byte>31) && (byte<127)) || (byte>159)) return(int)byte;
  26.   else return(int)'.';
  27. }
  28.  
  29. void hexconv(result,byte) /* byte to 2 ASCII hex digits */
  30. /*   -------                        */
  31. unsigned char result[2],byte;
  32. {
  33.   result[0]=(byte>>4)+48+7*((byte>>4)>9);
  34.   result[1]=(byte & 0xf)+48+7*((byte & 0xf)>9);
  35. }
  36.  
  37. void hexdump(fh, secbuf, len, addr)
  38. /*   -------             */
  39. FILE *fh;
  40. unsigned char *secbuf;
  41. long len;
  42. long addr;
  43. {
  44.   register int row,col,i;
  45.   register char strbuf[100];
  46.  
  47.   /* record display core routine : Due to the slowness of the
  48.      console file drivers, display is line buffered for speed. */
  49.  
  50.   for (row=0;row*16<len;row++) {
  51.     i=0;
  52.     for (col=0;row*16+col<len && col<16;col++) {
  53.       hexconv(&strbuf[i],secbuf[row*16+col]); i+=2;
  54.       if (((col+1)%4)==0) strbuf[i++]=' ';
  55.     }
  56.     while (i<36)
  57.       strbuf[i++] = ' ';
  58.     strbuf[i++]=':'; strbuf[i++]=' ';
  59.     for (col=0;row*16+col<len && col<16;col++)
  60.       strbuf[i++]=valid_display(secbuf[row*16+col]);
  61.  
  62.     strbuf[i]='\0';
  63.     fprintf(fh, "%06X: %s\n", addr, strbuf);
  64.     addr += 16;
  65.   }
  66. }
  67.  
  68. void longtoascii(l, str)
  69. /*   -----------*/
  70. ULONG l;
  71. char *str;
  72. {
  73. char *lptr;
  74. int i;
  75.  
  76. lptr = (char *)&l;
  77. for (i=0; i<4; i++)
  78.    *str++ = valid_display(*lptr++);
  79. *str = '\0';
  80. }
  81.  
  82. int locaddr(addr, info)
  83. /*  -------*/
  84. /* Find what hunk an address is in.  If hunk has a symbol chain, find    */
  85. /* offset from symbol, else return offset in hunk. Return false if    */
  86. /* address is not in any of the hunks.                    */
  87.  
  88. ULONG addr;
  89. struct addrinfo *info;
  90. {
  91. int i, j, offset, tabsize;
  92. struct symbol_node *symaddr;
  93. struct line_node *lineaddr;
  94. struct segment *seg;
  95. struct line_elem *curln;
  96.  
  97. info->name = 0;
  98. info->objname = 0;
  99.  
  100. for (i = 0; i < tbdata->segcount; i++)
  101.    {
  102.    seg = &tbdata->segments[i];
  103.    if ((seg->addr <= addr) && (addr < seg->addr + seg->size))
  104.       {
  105.       info->hunknum = i;
  106.       offset = info->offset = addr - seg->addr;
  107.  
  108.       symaddr = (struct symbol_node *)&(seg->symbols);
  109.       while (symaddr->sn_next)
  110.      {
  111.      if (symaddr->sn_next->sn_value <= addr)
  112.         info->name = symaddr->sn_next->sn_sym;
  113.      else
  114.         break;
  115.      symaddr = symaddr->sn_next;
  116.      }
  117.       if (info->name)
  118.      info->offset = addr - symaddr->sn_value;
  119.  
  120.       lineaddr = (struct line_node *)&(seg->lines);
  121.       while (lineaddr->ln_next)
  122.      {
  123.      if (lineaddr->ln_next->ln_offset <= offset)
  124.         info->objname = lineaddr->ln_next->ln_name;
  125.      else
  126.         break;
  127.      lineaddr = lineaddr->ln_next;
  128.      }
  129.       if (info->objname)
  130.      {
  131.      offset = offset - lineaddr->ln_offset;
  132.      curln = (struct line_elem *)&lineaddr->ln_name[lineaddr->ln_nsize<<2];
  133.      tabsize = (lineaddr->ln_size
  134.         - (sizeof(struct line_node) - 4 + (lineaddr->ln_nsize<<2)) >> 3);
  135.      for (j=1; j<=tabsize; j++)
  136.         {
  137.         if (curln->le_off > offset)
  138.            {
  139.            break;
  140.            }
  141.         curln += 1;
  142.         }
  143.      curln -= 1;  /* could die if first offset != 0???        */
  144.      info->line = curln->le_line;
  145.      info->lineoff = offset - curln->le_off;
  146.      if (j > tabsize)
  147.         if (info->lineoff > 32)     /* probably outside this file   */
  148.            info->objname = 0;    /* deny knowledge of line    */
  149.      }
  150.       return (1);
  151.       }
  152.    }
  153. return (0);
  154. }
  155.